#include "TGN.h"

#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

TGNFile::TGNFile() 
{
	loaded = false; 
	header.numAnis = 0;
	header.numLetters = 0;
	header.imageName = 0;
	header.ID = 0;

	for(int i = 0; i < MAX_ANIS; i++)
	{
		anis[i].numFrames = 0;

	}
}

int TGNFile::Load(char *filename)
{
	FILE *fp=fopen(filename, "rb");

	if(fp == NULL)
		return 0;

	fread(&header.ver, 1, 1, fp);
	fread(&header.ID, 1, 1, fp);
	fread(&header.numFrames, 1, 1, fp);
	fread(&header.numAnis, 1, 1, fp);
	fread(&header.alpha, 1, sizeof(int), fp);

	fread(&header.numLetters, 1, 1, fp);
	header.imageName = new char[header.numLetters];

	for(int i = 0; i < header.numLetters; i++)
	{
		fread(&header.imageName[i], 1, 1, fp);
	}

	for(int i = 0; i < header.numAnis; i++)
	{
		fread(&anis[i], 1, sizeof(TGNAni), fp);
	}

	fclose(fp);

	loaded = true;
	return 1;
}

int TGNFile::Save(char *filename)
{
	FILE *fp=fopen(filename, "wb");

	if(fp == NULL)
		return 0;

	fwrite(&header.ver, 1, 1, fp);
	fwrite(&header.ID, 1, 1, fp);
	fwrite(&header.numFrames, 1, 1, fp);
	fwrite(&header.numAnis, 1, 1, fp);
	fwrite(&header.alpha, 1, sizeof(int), fp);

	fwrite(&header.numLetters, 1, 1, fp);

	for(int i = 0; i < header.numLetters; i++)
	{
		fwrite(&header.imageName[i], 1, 1, fp);
	}

	for(int i = 0; i < header.numAnis; i++)
	{
		fwrite(&anis[i], 1, sizeof(TGNAni), fp);
	}

	fclose(fp);

	return 1;
}

int TGNFile::AddFrame(TGN_BYTE AniIndex, TGNFrame *newFrame)
{
	if(anis[AniIndex].numFrames >= MAX_FRAMES)
		return 0;

	TGN_BYTE cnt = anis[AniIndex].numFrames++;
	anis[AniIndex].frames[cnt] = *newFrame;

	return 1;
}

int TGNFile::SetFrame(TGN_BYTE AniIndex, TGN_BYTE FrmIndex, int x, int y, int w, int h, int delay)
{
	anis[AniIndex].frames[FrmIndex].x = x;
	anis[AniIndex].frames[FrmIndex].y = y;
	anis[AniIndex].frames[FrmIndex].w = w;
	anis[AniIndex].frames[FrmIndex].h = h;
	anis[AniIndex].frames[FrmIndex].delay = delay;

	return 1;
}

int TGNFile::SetHotspot(TGN_BYTE AniIndex, TGN_BYTE FrameIndex, int hx, int hy)
{
	if(AniIndex >= header.numAnis || FrameIndex >= anis[AniIndex].numFrames)
		return 0;

	anis[AniIndex].frames[FrameIndex].hotx = hx;
	anis[AniIndex].frames[FrameIndex].hoty = hy;

	return 1;
}

int TGNFile::SetBounds(TGN_BYTE AniIndex, TGN_BYTE FrameIndex, int bx, int by)
{
	if(AniIndex >= header.numAnis || FrameIndex >= anis[AniIndex].numFrames)
		return 0;

	anis[AniIndex].frames[FrameIndex].boundx = bx;
	anis[AniIndex].frames[FrameIndex].boundy = by;

	return 1;
}

int TGNFile::DeleteFrame(TGN_BYTE AniIndex, TGN_BYTE FrameIndex)
{
	if(anis[AniIndex].numFrames <= 0)
		return 0;

	for(int i = FrameIndex; i < MAX_FRAMES - 1; i++)
	{
		anis[AniIndex].frames[i] = anis[AniIndex].frames[i+1];
	}

	anis[AniIndex].numFrames--;

	return 1;
}

TGNFrame *TGNFile::GetFrame(TGN_BYTE AniIndex, TGN_BYTE FrameIndex)
{
	return &anis[AniIndex].frames[FrameIndex];
}

int TGNFile::AddAni(TGNAni *newAni)
{
	if(header.numAnis >= MAX_ANIS)
		return 0;

	anis[header.numAnis] = *newAni;

	header.numAnis++;

	return 1;
}

int TGNFile::DeleteAni(TGN_BYTE index)
{
	if(header.numAnis <= 0)
		return 0;

	for(int i = index; i < MAX_ANIS - 1; i++)
	{
		anis[index] = anis[index + 1];
	}

	header.numAnis--;

	return 1;
}

int TGNFile::DeleteAllAnis()
{
	TGNAni emptAni;
	memset(&emptAni, 0, sizeof(TGNAni));

	for(int i = 0; i < MAX_ANIS; i++)
		anis[i] = emptAni;

	header.numAnis = 0;

	return 1;
}

TGNAni *TGNFile::GetAni(TGN_BYTE index)
{
	return &anis[index];
}

int TGNFile::SetAni(TGN_BYTE index, TGN_BYTE ID, TGN_BYTE nextID, int boundw, int boundh, char *name)
{
	strncpy(anis[index].name, name, 9);
	anis[index].name[9] = '\0';

	anis[index].ID = ID;
	anis[index].nextID = nextID;
	anis[index].boundW = boundw;
	anis[index].boundH = boundh;

	return 1;
}

int TGNFile::SetImageName(char *filename)
{
	header.numLetters = strlen(filename) + 1;

	if(header.imageName != 0)
		delete [] header.imageName;

	header.imageName = new char[strlen(filename) + 1];
	header.imageName[strlen(filename)] = '\0';

	strcpy(header.imageName, filename);

	return 1;
}

char *TGNFile::GetImageName()
{
	return header.imageName;
}

void TGNFile::Clear()
{
	TGNHeader emptHeader;
	TGNAni emptAni;

	memset(&emptHeader, 0, sizeof(TGNHeader));
	memset(&emptAni, 0, sizeof(TGNAni));

	header = emptHeader;

	for(int i = 0; i < MAX_ANIS; i++)
	{
		anis[i] = emptAni;

	}

	loaded = false;
}

int TGNFile::SetAlpha(int newAlpha)
{
	header.alpha = newAlpha;
	return 1;
}

int TGNFile::SetID(int newID)
{
	header.ID = newID;
	return 1;
}

TGN_BYTE TGNFile::GetID()
{
	return header.ID;
}

int TGNFile::Unload()
{
	delete [] header.imageName;
	loaded = false;

	return 1;
}
